package com.baijiarui.myself;

import com.fc.test.common.base.BaseController;
import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
public class StudentController extends BaseController {
    @Autowired
    private ForeJobMapper foreJobMapper;
    @Autowired
    private ForeRecruitMapper foreRecruitMapper;
    @Autowired
    private ForeResumeMapper foreResumeMapper;


    //查询用户自己的求职信息列表
    @RequestMapping("/student/jobList")
    public Object resumeList(){
        TsysUser user = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        String userId=user.getId();
        ForeJobExample example=new ForeJobExample();
        example.or().andUserIdEqualTo(userId);
        List list=foreJobMapper.selectByExample(example);
        return list;
    }
    @RequestMapping("/student/zhaopingList")
    public Object zhaopingList(){
        List list=foreRecruitMapper.selectByExample(null);
        return list;
    }


    @RequestMapping("/student/jobAdd")
    public Object jobAdd(ForeJob foreJob,String time) throws ParseException {
        TsysUser user = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        String userId=user.getId();
        String name=user.getNickname();
        foreJob.setUserId(userId);
        foreJob.setName(name);
        foreJob.setId(UUID.randomUUID().toString());
        foreJob.setEffectiveTime(new SimpleDateFormat("yyyy-MM-dd").parse(time));
        //获取简历id
        ForeResumeExample example=new ForeResumeExample();
        example.or().andUserIdEqualTo(userId);
        List<ForeResume> list=foreResumeMapper.selectByExample(example);
        String resumeId=list.size()>0?list.get(0).getId():null;
        foreJob.setResumeId(resumeId);
        return foreJobMapper.insertSelective(foreJob)==1?"success":"fail";
    }

    @RequestMapping("/student/deleteJobById")
    public Object deleteById(String id){
        return foreJobMapper.deleteByPrimaryKey(id);
    }

    @RequestMapping("/student/findResume")
    public Object findResume(){
        TsysUser user = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        String userId=user.getId();
        ForeResumeExample example=new ForeResumeExample();
        example.or().andUserIdEqualTo(userId);
        List list=foreResumeMapper.selectByExample(example);
        return list.size()==0?new ForeResume():list.get(0);
    }
    @RequestMapping("/student/saveResume")
    public Object save(ForeResume resume,String birthdayStr,String finishTimeStr) throws ParseException {
        ForeResume temp= (ForeResume) findResume();
        resume.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthdayStr));
        resume.setFinishTime(new SimpleDateFormat("yyyy-MM-dd").parse(finishTimeStr));
        if(temp.getId()!=null){
            resume.setId(temp.getId());
           return foreResumeMapper.updateByPrimaryKeySelective(resume)==1?"success":"fail";
        }else{
            TsysUser user = (TsysUser) SecurityUtils.getSubject().getPrincipal();
            String userId=user.getId();
            resume.setUserId(userId);
            resume.setId(UUID.randomUUID().toString());

           return foreResumeMapper.insertSelective(resume)==1?"success":"fail";
        }
    }

    @RequestMapping("/student/findUserInfo")
    public Object findUserInfo(){
        TsysUser user = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        String userId=user.getId();
        ForeStudentExample example=new ForeStudentExample();
        example.or().andIdEqualTo(userId);
        List<ForeStudent> list=studentMapper.selectByExample(example);
        return list.get(0);
    }

    @Autowired
    public ForeStudentMapper studentMapper;
     @Autowired
     public TsysUserMapper tsysUserMapper;

    //MD5Util.encode
    @RequestMapping("/student/saveUserInfo")
    public Object saveUserInfo(ForeStudent student){
        TsysUser temp = (TsysUser) SecurityUtils.getSubject().getPrincipal();
        String userId=temp.getId();
        student.setId(userId);
       Integer i= studentMapper.updateByPrimaryKeySelective(student);
        //更新系统用户的密码和昵称
        temp.setPassword(MD5Util.encode(student.getPassword()));
        temp.setUsername(student.getUsername());
        temp.setNickname(student.getName());
        Integer i2=  tsysUserMapper.updateByPrimaryKeySelective(temp);

        return i+i2==2?"success":"fail";
    }
    @Autowired
    ForeCompanyMapper foreCompanyMapper;

    @RequestMapping("/student/findCompanyById")
    public Object findCompanyById(String id){
      return   foreCompanyMapper.selectByPrimaryKey(id);
    }

    @RequestMapping("/student/findJobById")
    public Object findJobById(String id){
      return   foreJobMapper.selectByPrimaryKey(id);
    }

    @RequestMapping("/student/updateJob")
    public Object findJobById(ForeJob foreJob,String effectiveTimeStr)throws  Exception{
        foreJob.setEffectiveTime(new SimpleDateFormat("yyyy-MM-dd").parse(effectiveTimeStr));
      return   foreJobMapper.updateByPrimaryKeySelective(foreJob);
    }






}
